set more off

use "reassessments", clear
	
merge m:m swis_code year using "switchers"

drop _m

merge m:m muni_name county_name year using "census demographics"

drop _m

merge m:m muni_name county_name year using "finances clean"

keep if year > 1986 & year < 2012


sort county_name muni_name
list county_name muni_name _m if _m != 3 & year == 2000

*** recode for interpretation ***

gen elected = 1-D
label variable elected "Elected assessor"
replace reass = reass * 100

keep if !mi(swis_code)
xtset swis_code year



#delimit;

local X1 
	lpopulation 
	lmedianincome 
	pct65plus 
	pctwhite 
	pctunemployed 
	pctrenter 
	pctagricuture
	;

#delimit cr

local X2 dltotalrevenue	pctptax

local X `X1' `X2' 

foreach x in `X' {
	su `x'
	replace `x' = (`x' - r(min)) / (r(max) - r(min))
}

*** do the regressions ***

eststo clear

*** baseline ***
eststo, title("Baseline"): areg reass elected i.year, a(swis_code) cl(swis_code)

*** alt control groups ***
eststo, title("Elected controls only"): areg reass elected i.year if switched == 1 | elected == 1, a(swis_code) cl(swis_code)
eststo, title("Appointed controls only"): areg reass elected i.year if switched == 1 | elected == 0, a(swis_code) cl(swis_code)
eststo, title("Switchers only"): areg reass elected i.year if switched == 1, a(swis_code) cl(swis_code)

*** no uncertain switchers ***
eststo, title("No uncertain switchers"): areg reass elected i.year if mi(Dyr_ub), a(swis_code) cl(swis_code)

*** effect by excluded year ***
preserve
	qui{
	gen B = .
	gen UB = .
	gen LB = .
	gen SE = .
	levelsof year, local(levels)
	foreach l of local levels {
		noisily di "`l'"
		areg reass elected i.year if year != `l', a(swis_code) cl(swis_code) 
		replace B = _b[elected] if year == `l'
		replace UB = _b[elected]+1.96*_se[elected] if year == `l'
		replace LB = _b[elected]-1.96*_se[elected] if year == `l'		
		replace SE = _se[elected] if year == `l'
	}

	gen absB = abs(B)
	sort absB
	gen index = _n
	su year if index == 1
	}
	eststo, title("Smallest effect dropping individual years and re-estimating"): xi: areg reass elected i.year if year != `r(mean)', a(swis_code) cl(swis_code) 
restore

*** effect by excluded county ***
preserve
	qui{
	encode county_name, gen(countyid)
	gen B = .
	gen UB = .
	gen LB = .
	gen SE = .
	levelsof countyid, local(levels)
	foreach l of local levels {
		noisily di "`l'"
		areg reass elected i.year if countyid != `l', a(swis_code) cl(swis_code) 
		replace B = _b[elected] if countyid == `l'
		replace UB = _b[elected]+1.96*_se[elected] if countyid == `l'
		replace LB = _b[elected]-1.96*_se[elected] if countyid == `l'		
		replace SE = _se[elected] if countyid == `l'
	}

	gen absB = abs(B)
	sort absB
	gen index = _n
	su countyid if index == 1
	}
	eststo, title("Smallest effect dropping individual counties and re-estimating"): xi: areg reass elected i.year if countyid != `r(mean)', a(swis_code) cl(swis_code) 
restore

*** print table ***

esttab, se nostar keep(elected) mtitles onecell

local myK = `r(nmodels)'
forvalues k = 1(1)`myK' {
	local mylab`k' = "(`k') " + r(m`k'_estimates_title)

}

matrix C = r(coefs)
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}




#delimit;

esttab 
	using "table A2.tex"
	,
		replace
		coeflabels(
			est1 "`mylab1'"
			est2 "`mylab2'"
			est3 "`mylab3'"
			est4 "`mylab4'"
			est5 "`mylab5'"
			est6 "`mylab6'"
			est7 "`mylab7'"
			est8 "`mylab8'"
			est9 "`mylab9'"
			est10 "`mylab10'"
			est11 "`mylab11'"
			est12 "`mylab12'"
		)
		noobs
		booktabs
		nonote
		cells(b(fmt(2) star) se(par))
		gap
		nomtitles
		nonum
		unstack
		b(2)
		mlabels("Estimate")
		collabels("(s.e.)")
		align(c)
		width(.8\hsize)
		;

#delimit cr
